package org.dromara.autoapi.utils;

import java.util.*;

public class TwoWordOperation {
    public static final Set<String> SYMBOL = Set.of(
            "eq", // 等于
            "gt", // 大于
            "ge", // 大于等于
            "lt", // 小于
            "le", // 大于等于
            "lr", // like right
            "ll", // like left
            "lk", // like
            "bw", // between and
            "in" // in
    );

    /**
     * 检查操作符合法性
     * @param word 前端传递的操作符
     */
    public static void checkSymbol(String word) {
        try {
            if (word.charAt(2) == ' ') {
                String symbol = word.substring(0, 2);
                if (SYMBOL.contains(symbol)) {
                    return;
                }
            }
        }catch (Exception ignored) {
        }
        throw new IllegalArgumentException("Invalid Symbol: " + word);
    }

    public static List<String> toWhereSql(Map<String,Object> where) {
        List<String> values = new ArrayList<>();

        List<String> template = new ArrayList<>();

        where.forEach((field,v)->{
            String value = v.toString();
            checkSymbol(value);
            String opr = value.substring(0,2);
            String val = value.substring(3);

            String part = switch (opr) {
                case "eq" -> {
                    values.add(val);
                    yield "%s = ?".formatted(field);
                }
                case "gt" -> {
                    values.add(val);
                    yield "%s > ?".formatted(field);
                }
                case "ge" -> {
                    values.add(val);
                    yield "%s >= ?".formatted(field);
                }
                case "lt" -> {
                    values.add(val);
                    yield "%s < ?".formatted(field);
                }
                case "le" -> {
                    values.add(val);
                    yield "%s <= ?".formatted(field);
                }
                case "lr" -> {
                    values.add(val);
                    yield field+" like concat(?, '%')";
                }
                case "ll" -> {
                    values.add(val);
                    yield field+" like concat('%', ?)";
                }
                case "lk" -> {
                    values.add(val);
                    yield field+" like concat('%', ? , '%')";
                }
                case "bw" -> {
                    String[] split = val.split(",");
                    if (split.length != 2) {
                        throw new IllegalArgumentException("Invalid value: " + val);
                    }
                    values.addAll(Arrays.stream(split).map(String::trim).toList());
                    yield "%s between ? and ?".formatted(field);
                }
                case "in" -> {
                    String[] split = val.split(",");
                    List<String> list = Arrays.stream(split).map(String::trim).toList();
                    values.addAll(list);
                    yield "%s in (%s)".formatted(field,String.join(",", Collections.nCopies(list.size(), "?")));
                }
                default -> throw new IllegalStateException("Unexpected value: " + value);
            };
            template.add(part);

        });

        values.add(0,String.join(" and ", template));
        return values;
    }

}
